from rich.console import Console
from rich.syntax import Syntax
from rich.panel import Panel
from rich.prompt import Prompt
import time

console = Console()

def code_editor():
    code = ""
    while True:
        console.print(
            Panel(
                Syntax(code, "python", theme="monokai", line_numbers=True),
                title="Python 编辑器",
                border_style="bright_cyan"
            )
        )
        action = Prompt.ask(
            "[green]操作:[/green] [bold red]q[/bold red]退出 [bold green]r[/bold green]运行 [bold blue]←[/bold blue]删除字符",
            choices=["q", "r", "←"],
            default="q"
        )
        if action == "q":
            break
        elif action == "r":
            try:
                exec(code)
                console.print("[green]执行成功[/green]")
            except Exception as e:
                console.print(f"[bold red]错误:[/bold red] {str(e)}")
        elif action == "←":
            code = code[:-1]

code_editor()